﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.Xml.Linq;
using System.Windows.Controls;
using DocumentManagement.Models;
//using System.Windows.Media;


namespace DocumentManagement.ViewsModels
{
   public class ShowItemDocumentInViewsModel : ViewsModelBase
    {
       public QSHCEntities1 _entityModels;
        public QSHCEntities1 EntityModels
        {
            get { return _entityModels; }
            set
            {
                if (_entityModels != value)
                {
                    _entityModels = value;
                    OnPropertyChanged("EntityModels");
                }
            }
        }

        public ICollectionView _myReceiveICV;
        public ICollectionView MyReceiveICV
        {
            get { return _myReceiveICV; }
            set
            {
                if (_myReceiveICV != value)
                {
                    _myReceiveICV = value;
                    OnPropertyChanged("MyReceiveICV");
                }
            }
        }





        public Receive _mySelectedEditItemReceive;
        public Receive MySelectedEditItemReceive
        {
            get { return _mySelectedEditItemReceive; }
            set
            {
                if (_mySelectedEditItemReceive != value)
                {
                    _mySelectedEditItemReceive = value;
                    OnPropertyChanged("MySelectedEditItemReceive");
                }
            }
        }

        //private string _myFilter;
        //public string MyFilter
        //{
        //    get { return _myFilter; }
        //    set
        //    {
        //        if (_myFilter != value)
        //        {
        //            _myFilter = value;
        //            OnPropertyChanged("MyFilter");
                  
        //        }
        //    }
        //}
     
    

       public ShowItemDocumentInViewsModel ()
    {
        

            if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                EntityModels = new QSHCEntities1();

                MyReceiveICV = CollectionViewSource.GetDefaultView(EntityModels.Receives.Where(p => p.Active == true).ToList()); //.Where(p => p.Active == true)
               
            }
    }

       public void DeleteFromDatabase()
       {

           var dr = MessageBox.Show("ต้องการลบข้อมูลหรือไม่", "ยืนยันการลบข้อมูล", MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.OK);
           if (dr == MessageBoxResult.OK)
           {
               var deleteReceive = (EntityModels.Receives.Where(p => p.ReceiveID == MySelectedEditItemReceive.ReceiveID).Single());
               deleteReceive.Active = false;

               foreach (var item in EntityModels.ReceiveAttachedFiles)
               {
                   var a = item.ReceiveID;
                   if (a == MySelectedEditItemReceive.ReceiveID)
                   {
                       item.Active = false;

                   }
               }
               EntityModels.SaveChanges();

               MyReceiveICV = CollectionViewSource.GetDefaultView(EntityModels.Receives.Where(p => p.Active == true).ToList());
               MyReceiveICV.Refresh();


           }
       }
       //public void RefreshData()
       //{
       //    MyReceiveICV = CollectionViewSource.GetDefaultView(EntityModels.Receives.Where(p => p.Active == true).ToList());
       //}


       public void Filter(string txtFilter, DateTime? startdate, DateTime? enddate)
       {
           
              MyReceiveICV = CollectionViewSource.GetDefaultView(EntityModels.Receives.Where(p => p.Active == true).ToList());
           
          
           MyReceiveICV.Filter += (o) =>
           {
               Receive p = (o as Receive);

               if (p == null)

                   return false;
           
                   if (txtFilter != "" && startdate == null && enddate == null)
                   {
                       if (p.ReceiveCode.Contains(txtFilter) || p.Title.Contains(txtFilter))
                           return true;
                       else
                           return false;
                   }

                   if (startdate != null && enddate != null && txtFilter == "")
                   {
                       if (p.ReceiveDate.Date >= startdate.Value.Date && p.ReceiveDate.Date <= enddate.Value.Date)
                           return true;
                       else
                           return false;
                   }

                   if (startdate != null && enddate == null && txtFilter == "")
                   {
                       if (p.ReceiveDate.Date >= startdate.Value.Date)
                           return true;
                       else
                           return false;
                   }

                   if (enddate != null && startdate == null && txtFilter == "")
                   {
                       if (p.ReceiveDate.Date <= enddate.Value.Date)
                           return true;
                       else
                           return false;
                   }

                   if (txtFilter != "" && startdate != null && enddate == null)
                   {
                       if ((p.ReceiveCode.Contains(txtFilter) || p.Title.Contains(txtFilter)) && p.ReceiveDate.Date >= startdate.Value.Date)
                           return true;
                       else
                           return false;
                   }

                   if (txtFilter != "" && startdate == null && enddate != null)
                   {
                       if ((p.ReceiveCode.Contains(txtFilter) || p.Title.Contains(txtFilter)) && p.ReceiveDate.Date <= enddate.Value.Date)
                           return true;
                       else
                           return false;
                   }

                   if (startdate != null && enddate != null && txtFilter != "")
                   {

                       if ((p.ReceiveCode.Contains(txtFilter) || p.Title.Contains(txtFilter)) && (p.ReceiveDate.Date >= startdate.Value.Date && p.ReceiveDate.Date <= enddate.Value.Date))
                           return true;
                       else
                           return false;

                   }
               
               return true;
        
           };
       }


    }
}
